let util_js = require("utils/util.js");
let sys_js = require("utils/system.js");
let wxp = require("utils/wxp.js");
let ws_js = require("/utils/socket.js");

App({
  onLaunch: function () {
    //初始化数据
    this.initData();
    //获取小程序的屏幕高度等信息
    //微信登录
    // this.wxLogin();
    // 初始化长连接
    this.initWebSocket();
  },

  // 初始化数据
  initData: function () {
    this.checkCusNav();
  },

  //查看是否支持自定义导航栏
  checkCusNav: function () {
    try {
      const res = wx.getSystemInfoSync();
      //保存
      sys_js.saveSystemInfo(res);
      //比较是否支持自定义导航栏
      if (util_js.compareWechatVersion(res.version, '6.6.0') < 0) {
        //1.set data
        this.globalData.canCusNav = false;
      }
    } catch (e) {
      console.log(e);
    }
  },

  //微信登录
  wxLogin: function () {
    wx.checkSession({
      success: res => {
        //session_key 未过期,正常使用
      },
      fail: res => {
        //获取code并缓存，有可能出问题，采取必要页面获取
      }
    });
  },

  // 初始化长连接
  initWebSocket() {
    wxp.getNetworkType().then(res => {
      let networkType = res.networkType;
      if (networkType != "none") {
        ws_js.webSocket.init();
      }
      console.log("网络类型" + networkType);
    }).catch(err => {
      console.log(err)
    });
    // 监听网络状态改变
    wx.onNetworkStatusChange(res => {
      let networkType = res.networkType;
      console.log("网络状态改变" + networkType);
      if (networkType != "none") {
        ws_js.webSocket.init();
      }
    })
  },

  globalData: {
    canCusNav: true
  }
})